<template>
  <div class="shopCart">
    <div class="content">
      <div class="content-l" @click="showCartList">
        <div class="icon-wrap" :class="{highlight: totalCount > 0}">
          <span class="icon-shopping_cart"></span>
          <div v-show="totalCount > 0" class="count">{{totalCount}}</div>
        </div>
        <div class="price">¥ {{totalPrice}}</div>
        <div class="desc">另需配送费{{deliveryPrice}}元</div>
      </div>
      <div class="content-r" :class="{highlight:totalPrice >= minPrice}">
        <!-- <span v-if="totalPrice < minPrice">¥ {{minPrice}}起送</span> -->
        <span>{{payDesc}}</span>
      </div>
      <div class="ball-container">
        <transition-group
          name="drop"
          tag="div"
          @before-enter="dropBeforeEnter"
          @enter="dropEnter"
          @after-enter="dropAfterEnter"
        >
          <div v-for="(item,i) in balls" :key="i" v-show="item.show" class="ball">
            <div class="inner"></div>
          </div>
        </transition-group>
      </div>
    </div>
    <transition name="fold">
      <div class="shopcart-list" v-show="isShowList">
        <div class="list-header">
          <h1 class="title">购物车</h1>
          <span class="empty" @click="emptyCart">清空</span>
        </div>
        <div class="list-content" ref="cartList">
          <div class="list-wrapper">
            <div class="good-item" v-for="(item,i) in selectFoods" :key="i">
              <div class="name">{{item.name}}</div>
              <div class="operation">
                <div class="price">
                  <span class="icon">¥</span>
                  {{item.price}}
                </div>
                <cart-control @setSelectFoods="filterSelectFoods" :food="item"></cart-control>
              </div>
            </div>
          </div>
        </div>
      </div>
    </transition>

    <div class="mask" v-show="isShowList" @click="showCartList"></div>
  </div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
import BScroll from 'better-scroll'
import cartControl from '../cartControl/CartControl'
export default {
  data() {
    return {
      balls: [
        { show: false },
        { show: false },
        { show: false },
        { show: false },
        { show: false }
      ],
      dropBalls: [],
      isShowList: false,
      listScroll: null
    }
  },
  props: {
    minPrice: {
      default: 0
    },
    deliveryPrice: {
      default: 0
    }
  },
  components: {
    cartControl
  },
  created() {},
  methods: {
    ...mapMutations(['filterSelectFoods', 'emptyCart']),
    initBScroll() {
      this.listScroll = new BScroll(this.$refs.cartList, {
        probeType: 3,
        click: true
      })
    },
    showCartList() {
      if (!this.totalCount) {
        this.isShowList = false
        return false
      }
      this.isShowList = !this.isShowList
      if (this.isShowList) {
        this.$nextTick(() => {
          if (!this.listScroll) {
            this.initBScroll()
          } else {
            this.listScroll.refresh()
          }
        })
      }
    },
    drop(el) {
      var index = this.balls.findIndex(item => item.show === false)
      if (index !== -1) {
        if (!this.balls[index].show) {
          this.balls[index].show = true
          this.balls[index].el = el
          this.dropBalls.push(this.balls[index])
        }
      }
    },
    dropBeforeEnter(el) {
      let count = this.balls.length
      while (count--) {
        var ball = this.balls[count]
        if (ball.show) {
          var rect = ball.el.getBoundingClientRect()
          var x = rect.left - 32
          // var y = -(window.innerHeight - rect.top - 22)
          el.style.display = ''
          el.style.webkitTransform = `translate3d(0,${-rect.top}px,0)`
          el.style.transform = `translate3d(0,${-rect.top}px,0)`
          var inner = el.getElementsByClassName('inner')[0]
          inner.style.webkitTransform = `translate3d(${x}px,0,0)`
          inner.style.transform = `translate3d(${x}px,0,0)`
        }
      }
    },
    dropEnter(el, done) {
      // 刷新当前屏幕
      /* eslint-disable no-unused-vars */
      var lf = el.offsetHeight
      this.$nextTick(() => {
        el.style.webkitTransform = 'trnaslate3d(0,0,0)'
        el.style.transform = 'trnaslate3d(0,0,0)'
        var inner = el.getElementsByClassName('inner')[0]
        inner.style.webkitTransform = 'trnaslate3d(0,0,0)'
        inner.style.transform = 'trnaslate3d(0,0,0)'
      })
    },
    dropAfterEnter(el) {
      var ball = this.dropBalls.shift()
      if (ball) {
        ball.show = false
        ball.el = null
      }
    }
  },
  computed: {
    ...mapState(['selectFoods']),
    totalPrice() {
      var price = 0
      this.selectFoods.forEach(item => {
        price += item.price * item.count
      })
      return price
    },
    totalCount() {
      var count = 0
      this.selectFoods.forEach(item => {
        count += item.count
      })
      return count
    },
    payDesc() {
      if (this.totalPrice === 0) {
        return `¥ ${this.minPrice}元起送`
      } else if (this.totalPrice < this.minPrice) {
        return `还差¥${this.minPrice - this.totalPrice}元起送`
      } else {
        return '去结算'
      }
    }
  }
}
</script>
<style lang="stylus" scoped>
.shopCart {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1.04rem;
  z-index: 40;

  .content {
    height: 100%;
    display: flex;
    background-color: #141d27;

    .content-l {
      flex: 1;
      display: flex;

      .icon-wrap {
        position: relative;
        height: 0.88rem;
        width: 0.88rem;
        border: 0.12rem solid rgb(20, 29, 39);
        border-radius: 50%;
        background-color: rgba(255, 255, 255, 0.4);
        margin-top: -6px;
        text-align: center;
        margin-left: 0.24rem;

        &.highlight {
          background-color: rgb(0, 160, 220);
          color: #fff;
        }

        .icon-shopping_cart {
          line-height: 0.88rem;
          font-size: 0.48rem;
        }

        .count {
          position: absolute;
          right: 0;
          top: -0.12rem;
          color: #fff;
          height: 0.32rem;
          line-height: 0.32rem;
          background-color: rgb(240, 20, 20);
          padding: 0 0.12rem;
          border-radius: 0.32rem;
          font-size: 0.18rem;
          font-weight: 700;
          box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.4);
          display: block;
        }
      }

      .price {
        align-self: center;
        font-size: 0.32rem;
        color: rgba(255, 255, 255, 0.4);
        font-weight: 700;
        height: 0.48rem;
        line-height: 0.48rem;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
        padding: 0 0.24rem;
      }

      .desc {
        align-self: center;
        font-size: 0.32rem;
        color: rgba(255, 255, 255, 0.4);
        padding-left: 0.24rem;
      }
    }

    .content-r {
      flex: 0 0 2.1rem;
      color: rgba(255, 255, 255, 0.4);
      font-weight: 700;
      font-size: 0.24rem;
      background-color: rgb(43, 51, 59);
      text-align: center;
      line-height: 1.04rem;

      &.highlight {
        color: #fff;
        background-color: rgb(27, 162, 88);
      }
    }

    .ball-container {
      height: 200;
      width: 100;
      background-color: #fff;

      .ball {
        position: fixed;
        left: 32px;
        bottom: 22px;
        z-index: 101;
        transform: translate3d(0, 0, 0);
        transition: all 0.4s cubic-bezier(0.66, 0.1, 1, 0.41);

        .inner {
          width: 0.32rem;
          height: 0.32rem;
          border-radius: 50%;
          background-color: rgb(0, 160, 220);
          transform: translate3d(0, 0, 0);
          transition: all 0.4s linner;
        }
      }
    }
  }

  .shopcart-list {
    position: absolute;
    bottom: 1.04rem;
    left: 0;
    width: 100%;
    height: 5.06rem;
    z-index: -1;
    background-color: #fff;
    transition: all 0.5s;
    padding-bottom: 0.9rem;

    &.fold-transition {
      transform: translate(0, 0);
    }

    &.fold-enter, &.fold-leave-to {
      transform: translate(0, 100%);
    }

    .list-header {
      height: 0.8rem;
      width: 100%;
      line-height: 0.8rem;
      background-color: #f3f5f7;
      display: flex;
      justify-content: space-between;
      padding: 0 0.24rem;
      box-sizing: border-box;
      border-bottom: 1px solid rgba(7, 17, 27, 0.1);

      .title {
        font-size: 0.28rem;
      }

      .empty {
        color: rgb(0, 160, 220);
        font-size: 0.24rem;
        cursor: pointer;
        line-height: 0.8srem;
      }
    }

    .list-content {
      box-sizing: border-box;
      padding: 0 0.24rem;
      overflow: hidden;
      height: 100%;

      .good-item {
        height: 0.96rem;
        width: 100%;
        border-bottom: 1px solid rgba(7, 17, 27, 0.1);
        display: flex;
        align-items: center;
        justify-content: space-between;

        .name {
          font-size: 0.28rem;
          color: rgb(7, 17, 27);
          font-weight: 600;
        }

        .operation {
          display: flex;
          align-items: center;

          .price {
            font-size: 0.28rem;
            margin-right: 0.18rem;
            color: rgb(240, 20, 20);
            font-weight: 700;

            .icon {
              font-size: 0.2rem;
              font-weight: 400;
            }
          }
        }
      }
    }
  }

  .mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -2;
    background-color: rgba(7, 17, 27, 0.6);
    filter: blur(10px);
  }
}
</style>
